#include "timer.h"

/*-----------------------------------         TIM2: 电机PWM         -------------------------------------*/
TIM_HandleTypeDef htim2, htim4;                        //定时器2初始化句柄
TIM_OC_InitTypeDef sConfigOC;

//定时器时钟与引脚配置
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
{
  GPIO_InitTypeDef GPIO_Initure;
  __HAL_RCC_TIM2_CLK_ENABLE();
  __HAL_RCC_TIM3_CLK_ENABLE();
  __HAL_RCC_TIM4_CLK_ENABLE();
  __HAL_RCC_GPIOF_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();

  /**********************舵机PWM引脚初始化  PD12-PD15****************/
  GPIO_Initure.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
  GPIO_Initure.Mode = GPIO_MODE_AF_PP;
  GPIO_Initure.Pull = GPIO_PULLDOWN;
  GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;
  GPIO_Initure.Alternate = GPIO_AF2_TIM4;
  HAL_GPIO_Init(GPIOD, &GPIO_Initure);

  /**********************电机PWM引脚初始化  PA0-PA3******************/
  GPIO_Initure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3;
  GPIO_Initure.Mode = GPIO_MODE_AF_PP;
  GPIO_Initure.Pull = GPIO_PULLUP;
  GPIO_Initure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_Initure.Alternate = GPIO_AF1_TIM2;
  HAL_GPIO_Init(GPIOA, &GPIO_Initure);

  /**********************蜂鸣器PWM引脚初始化  PB5********************/
  GPIO_Initure.Pin = GPIO_PIN_5;
  GPIO_Initure.Mode = GPIO_MODE_AF_PP;
  GPIO_Initure.Pull = GPIO_NOPULL;
  GPIO_Initure.Speed = GPIO_SPEED_FREQ_HIGH;
  GPIO_Initure.Alternate = GPIO_AF2_TIM3;
  HAL_GPIO_Init(GPIOB, &GPIO_Initure);

  /**********************电机方向控制引脚初始化**********************/
  GPIO_Initure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5;
  GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_Initure.Pull = GPIO_PULLUP;
  GPIO_Initure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  HAL_GPIO_Init(GPIOF, &GPIO_Initure);

  GPIO_Initure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3;
  GPIO_Initure.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_Initure.Pull = GPIO_PULLUP;
  GPIO_Initure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  HAL_GPIO_Init(GPIOC, &GPIO_Initure);

  HAL_GPIO_WritePin(GPIOF, GPIO_PIN_4, GPIO_PIN_SET);
}


/* 电机控制，TIM2CH1, TIM2CH3 */
static void timer2_motor_pwm_init(void)
{
  htim2.Instance = TIM2;
  htim2.Init.Period = 1000 - 1;
  htim2.Init.Prescaler = 84 - 1;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  HAL_TIM_PWM_Init(&htim2);

  sConfigOC.OCMode = TIM_OCMODE_PWM1;                                   //配置TIM2 CH1为PWM1模式
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;                           //输出比较极性为高
  sConfigOC.Pulse = 64;
  HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);          //PWM初始化
  HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);          //PWM初始化
  HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3);          //PWM初始化
  HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4);          //PWM初始化

  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);                            //开启定时器2PWM波输出
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);                            //开启定时器2PWM波输出
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);                            //开启定时器2PWM波输出
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4);                            //开启定时器2PWM波输出
}




/*-----------------------------------         TIM4: 舵机PWM         -------------------------------------*/
/* tim4 PWM输出 控制舵机 */
static void timer4_servo_pwm_init(void)
{
  htim4.Instance = TIM4;
  htim4.Init.Prescaler = 84 - 1;
  htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim4.Init.Period = 20000;
  htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  HAL_TIM_PWM_Init(&htim4);

  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.Pulse = 64;
  HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_1);
  HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_2);
  HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_3);
  HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_4);

  HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1);
  HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_2);
  HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);
  HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4);

  //初始化位置
  __HAL_TIM_SET_COMPARE(&htim4, left_servo, 780);    //增大，顺时针
  __HAL_TIM_SET_COMPARE(&htim4, right_servo, 1500);  //增大，顺时针
  __HAL_TIM_SET_COMPARE(&htim4, folder_servo, 1100);//增大，夹紧
  __HAL_TIM_SET_COMPARE(&htim4, lift_servo, 1500);  //
}


void timer_folder_pwm_set(u16 p)
{
  __HAL_TIM_SET_COMPARE(&htim4, lift_servo, p);
}


/*-----------------------------------         TIM3: 蜂鸣器PWM         -------------------------------------*/

TIM_HandleTypeDef TIM3_Handler;         //定时器3PWM句柄 
TIM_OC_InitTypeDef TIM3_CH2Handler;      //定时器3通道4句柄

static void timer3_beep_pwm_init(u16 arr, u16 psc)
{
  TIM3_Handler.Instance = TIM3;            //定时器3
  TIM3_Handler.Init.Prescaler = psc;       //定时器分频
  TIM3_Handler.Init.CounterMode = TIM_COUNTERMODE_UP;//向上计数模式
  TIM3_Handler.Init.Period = arr;          //自动重装载值
  TIM3_Handler.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  HAL_TIM_PWM_Init(&TIM3_Handler);       //初始化PWM

  TIM3_CH2Handler.OCMode = TIM_OCMODE_PWM1; //模式选择PWM1
  TIM3_CH2Handler.Pulse = 0;            //设置比较值,此值用来确定占空比，默认比较值为自动重装载值的一半,即占空比为50%
  TIM3_CH2Handler.OCPolarity = TIM_OCPOLARITY_LOW; //输出比较极性为低
  HAL_TIM_PWM_ConfigChannel(&TIM3_Handler, &TIM3_CH2Handler, TIM_CHANNEL_2);//配置TIM3通道4

  HAL_TIM_PWM_Start(&TIM3_Handler, TIM_CHANNEL_2);//开启PWM通道4
}


void tim_set_pwm_buzzer(u32 compare)
{
  TIM3->CCR2 = compare;
}


/*--------------------------------  用到的定时器初始化  -----------------------------*/

void timer_init(void)
{
  timer3_beep_pwm_init(1000, 84);
  timer2_motor_pwm_init();
  timer4_servo_pwm_init();
}





